<template>
  <el-dialog :title="info.title" :visible.sync="visible" center width="760px" :before-close="handleCancel">
    <el-form ref="formDate" label-suffix=":" label-width="100px" :rules="rules" :model="form">
      <el-row>
        <el-col :span="12">
          <el-form-item label="名称" prop="name" style="width: 100%">
            <el-input v-model="form.name" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="注册资金" prop="registered_capital" style="width: 100%">
            <el-input v-model="form.registered_capital" size="small" clearable style="width: 80%" /> 万元
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="信用代码" prop="credit_code" style="width: 100%">
            <el-input v-model="form.credit_code" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结算周期" prop="settlement_period" style="width: 100%">
            <el-input v-model="form.settlement_period" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="付款方式" prop="payment" style="width: 100%">
            <el-select v-model="form.payment" size="small" style="width: 100%">
              <el-option
                v-for="item in types"
                :key="item.key"
                :label="item.value"
                :value="item.key"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系人" prop="contacts" style="width: 100%">
            <el-input v-model="form.contacts" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电话" prop="phone" style="width: 100%">
            <el-input v-model="form.phone" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="地址" prop="addr" style="width: 100%">
            <el-input v-model="form.addr" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开户行" prop="bank" style="width: 100%">
            <el-input v-model="form.bank" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="账号" prop="account" style="width: 100%">
            <el-input v-model="form.account" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="邮箱" prop="mailbox" style="width: 100%">
            <el-input v-model="form.mailbox" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="等级" prop="rate" style="width: 100%">
            <el-select v-model="form.rate" size="small" style="width: 100%">
              <el-option
                v-for="item in rates"
                :key="item.key"
                :label="item.value"
                :value="item.key"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="主营业务" prop="main_business" style="width: 100%">
            <el-input type="textarea" :rows="4" v-model="form.main_business" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark" style="width: 100%">
            <el-input type="textarea" :rows="4" v-model="form.remark" size="small" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="fr mt10">
            <el-button size="small" @click="handleCancel()">取 消</el-button>
            <el-button type="primary" size="small" @click="handleEdit()">保 存</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { updSup, payments } from '@/api/supplier'
export default {
  props: {
    info: {
      type: Object,
      default: null
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  data() {
    return {
      form: {},
      types: [],
      rates: [{ key: 'A', value: 'A' }, { key: 'B', value: 'B' }, { key: 'C', value: 'C' }, { key: 'D', value: 'D' }],
      rules: {
        name: [{ required: true, trigger: 'blur', message: '供应商名称不可为空' }],
        registered_capital: [{ required: true, trigger: 'blur', message: '注册资金不可为空' }],
        credit_code: [{ required: true, trigger: 'blur', message: '统一信用代码不可为空' }],
        main_business: [{ required: true, trigger: 'blur', message: '主营业务不可为空' }],
        settlement_period: [{ required: true, trigger: 'blur', message: '结算周期不可为空' }],
        payment: [{ required: true, trigger: 'blur', message: '付款方式不可为空' }],
        rate: [{ required: true, trigger: 'blur', message: '等级不可为空' }],
        contacts: [{ required: true, trigger: 'blur', message: '联系人不可为空' }],
        phone: [{ required: true, trigger: 'blur', message: '电话不可为空' }],
        addr: [{ required: true, trigger: 'blur', message: '地址不可为空' }],
        bank: [{ required: true, trigger: 'blur', message: '开户行不可为空' }],
        account: [{ required: true, trigger: 'blur', message: '账号不可为空' }],
        mailbox: [{ required: true, trigger: 'blur', message: '邮箱不可为空' }]
      },
      visible: false
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.form = this.info.edit
        this.payments()
      }
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  mounted() {
  },
  methods: {
    payments() {
      payments().then(response => {
        this.types = response.list
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    handleEdit() {
      this.$refs['formDate'].validate(async(valid) => {
        if (valid) {
          updSup(this.form).then(response => {
            this.getData()
            this.handleCancel()
            this.$message.success('编辑成功')
          }).catch(err => {
            this.$message.error(err.msg)
          })
        }
      })
    },
    handleCancel(done) {
      if (this.$refs['formDate']) this.$refs['formDate'].resetFields()
      if (done && typeof done === Function) {
        done()
      } else {
        this.visible = false
      }
    }
  }
}
</script>

<style>

</style>
